<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cart;
use DB;
use App\Model\Goods;
class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   //取出购物车数据
        $carts = Cart::content();
        // dd($carts);
        //购物车总数
        $count = Cart::count();
        $pay = Cart::subtotal();
        
        return view('home.cart_index')->with('carts',$carts)->with('count',$count)->with('pay',$pay);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 移出购物车
     */
    public  function removeItem($rowId)
    {
        Cart::remove($rowId);
        return redirect('/cart');
    }

     /**
     * Remove the specified resource from storage.
     *清空购物车
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delCart()
    {
        $res = Cart::destroy();
        return redirect('/');
    }

    /**
     * Remove the specified resource from storage.
     *添加商品到购物车
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function addCart($goods_id,$num){
        $good = DB::table('goods')->where('goods_id','=',$goods_id)->first();
        Cart::add($good->goods_id,$good->goods_name,$num,$good->shop_price,['goods_price'=>$good->goods_price]);
        return redirect('cart');
    }
}
